package com.ruoyi.finance.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.domain.Project;
import com.ruoyi.finance.domain.FPayment;
import com.ruoyi.finance.model.FExperiment;
import com.ruoyi.finance.model.Ids;
import com.ruoyi.finance.model.Pfinance;
import com.ruoyi.finance.model.Query;
import com.ruoyi.finance.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class FinanceController extends BaseController {

    @Autowired
    FinanceService service;

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Pfinance project)
    {
        startPage();
        List<Pfinance> list = service.getAllItem(project);
        return getDataTable(list);
    }

    /**
     * 获取付款批次
     * @param pid
     * @return
     */
    @GetMapping("/list/one")
    public AjaxResult getOne(Long pid){
        return success(service.getPaymentByPid(pid));
    }

    /**
     * 到账
     */
    @PostMapping("/getpay")
    public AjaxResult getPay(@RequestBody FPayment payment){
        return success(service.getPay(payment));
    }

    /**
     * 全部到账
     * @param pid
     * @return
     */
    @GetMapping("/finish")
    public AjaxResult finish(Long pid,Integer finish){
        service.finishPrice(pid,finish);
        return success();
    }
    @GetMapping("/backpay")
    public AjaxResult backPay(Long id,Long pid){
        return success(service.backPay(id,pid));
    }

    /**
     * 导出实验管理列表
     */
//    @PreAuthorize("@ss.hasPermi('experiment:experiment:export')")
    @Log(title = "实验管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pfinance project)
    {
        List<Pfinance> list = service.getAllItem(project);
        ExcelUtil<Pfinance> util = new ExcelUtil<Pfinance>(Pfinance.class);
        util.exportExcel(response, list, "实验管理数据");
    }

    @GetMapping("/experiment/list4")
    public TableDataInfo expList4(Query query){
        startPage();
        List<Experiment> expByProjectId = service.getExpByProjectId(query,4);
        return getDataTable(expByProjectId);
    }
    @GetMapping("/experiment/list5")
    public TableDataInfo expList5(Query query){
        startPage();
        List<Experiment> expByProjectId = service.getExpByProjectId(query,5);
        return getDataTable(expByProjectId);
    }
    @GetMapping("/experiment/check")
    public AjaxResult check(Long id){
        service.check(id);
        return success();
    }
    @GetMapping("/experiment/back")
    public AjaxResult back(Long id){
        service.back(id);
        return success();
    }
    @PostMapping("/experiment/checks")
    public AjaxResult checks(@RequestBody Ids ids){
        for (Long id : ids.getIds()) {
            service.check(id);
        }
        return success();
    }
    @PostMapping("/experiment/backs")
    public AjaxResult backs(@RequestBody Ids ids){
        for (Long id : ids.getIds()) {
            service.back(id);
        }
        return success();
    }

    @PostMapping("/experiment")
    public TableDataInfo getExpeList(@RequestBody FExperiment pe){
        startPage();
        List<FExperiment> list = service.getAllExpByPid(pe);
        return getDataTable(list);
    }
}
